<!--
 * @描述: 角色管理的公共组件
 * @版权所有: 广东国星科技有限公司
 * @商业授权: www.mscodecloud.com
-->
<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.roleName" :placeholder="$t('sysrole.roleName')" clearable class="filter-input filter-item" @keyup.enter.native="handleFilter" />
      <el-button v-waves type="primary" class="filter-item" icon="el-icon-search" @click="handleFilter">
        {{ $t('common.query') }}
      </el-button>
      <el-button v-waves type="primary" class="filter-item" icon="el-icon-refresh-right" @click="handleReset">
        {{ $t('common.reset') }}
      </el-button>
    </div>
    <el-table
      :key="tableKey"
      ref="multipleTable"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%"
      @sort-change="sortChange"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="40" />
      <el-table-column :label="$t('sysrole.roleCode')" prop="roleCode" />
      <el-table-column :label="$t('sysrole.roleName')" prop="roleName" />
      <el-table-column :label="$t('sysrole.roleDescription')" prop="roleDescription" />
      <el-table-column :label="$t('common.create-time')" min-width="100px" prop="createTime" sortable="custom" :class-name="getSortClass('createTime')">
        <template slot-scope="{row}">
          <span>{{ row.createTime | formatDate }}</span>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.currentPage" :limit.sync="listQuery.pageSize" @pagination="getList" />

    <div slot="footer" class="dialog-footer" style="float: right">
      <el-button @click="hideSysRoleComponent()">
        {{ $t('common.cancel') }}
      </el-button>
      <el-button type="primary" @click="createSysRoleComponentMsg()">
        {{ $t('common.confirm') }}
      </el-button>
    </div>
  </div>
</template>

<script>
import { querySysRole } from '@/api/admin'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination

export default {
  name: 'SysRoleComponent',
  components: { Pagination },
  directives: { waves },
  props: ['msg'], // eslint-disable-line
  data() {
    return {
      leftIcon: true,
      rightIcon: false,
      tableKey: 0,
      labelPosition: 'right',
      list: [],
      total: 0,
      treeData: this.$store.getters.treeData,
      listLoading: true,
      listQuery: {
        currentPage: 1,
        pageSize: 10,
        roleName: undefined,
        status: undefined,
        orgId: undefined,
        orgName: undefined,
        sorter: 'createTime_descend'
      },
      orgName: '',
      multipleSelection: [],
      dialogStatus: '',
      textMap: {
        update: this.$t('common.edit'),
        create: this.$t('common.add')
      }
    }
  },
  watch: {
    msg(oldValue, newValue) { }
  },
  created() {
    if (window.innerWidth < 700) {
      this.labelPosition = 'top'
    }
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      querySysRole(this.listQuery).then(response => {
        this.list = response.data.list
        this.total = response.data.pagination.total
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.currentPage = 1
      this.listQuery.orgId = ''
      this.listQuery.orgName = ''
      this.getList()
    },
    handleReset() {
      this.listQuery.roleName = ''
      this.listQuery.status = ''
      this.listQuery.orgId = ''
      this.listQuery.orgName = ''
      this.handleFilter()
    },
    sortChange(data) {
      const { prop, order } = data
      if (prop === 'createTime') {
        this.sortByCreateTime(order)
      }
    },
    sortByCreateTime(order) {
      if (order === 'ascending') {
        this.listQuery.sorter = 'createTime_ascend'
      } else {
        this.listQuery.sorter = 'createTime_descend'
      }
      this.handleFilter()
    },
    handleSelectionChange(val) {
      if (this.msg === 'single') {
        if (val.length > 1) {
          this.$refs.multipleTable.clearSelection()
          this.$refs.multipleTable.toggleRowSelection(val.pop())
        } else {
          this.multipleSelection = val
        }
      } else {
        this.multipleSelection = val
      }
    },
    hideSysRoleComponent() {
      this.$emit('hideSysRoleComponent', 1)
    },
    createSysRoleComponentMsg() {
      this.$emit('sysRoleComponentMsg', this.multipleSelection)
      this.$emit('hideSysRoleComponent', 1)
    },
    getSortClass: function(key) {
      const sorter = this.listQuery.sorter
      return sorter === `${key}_ascend` ? 'ascending' : 'descending'
    },
    handleWarning(response) {
      this.$message({
        message: response.message || response,
        type: 'warning',
        duration: 2000
      })
    }
  }
}
</script>
<style scoped>
  .input-with-select .el-input-group__prepend {
    background-color: #fff;
  }
  .el-table .cell {
    white-space: nowrap;
    text-overflow: unset;
    margin-right: 10px;
  }
  ::-webkit-scrollbar {
    width: 8px;
    height: 8px;
  }
  ::-webkit-scrollbar-thumb {
    background-color: #C0D4F0;
    border-radius: 3px;
  }
  .pagination-container[data-v-72233bcd] {
    padding: 0
  }
</style>
